﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServerApplication.ConfigFile
{
    class ConfigFileObject
    {
        public int AmountPeers = 5;
        public int peerIndex;
        public string gatekeeperIp = "127.0.0.1";
        public int pathNumber = 3;
        public string outputFileName = "";
        public float intervalForBenchmark = 0.200f;
        public float timeAfterCommAttempt = 0.250f;
        public float timeSleepAfterFailedAttempt = 0.500f;
        public int useNetwork = 1;
        public int socketNumber = 20012;
        public string peerName = "Tester";
        public int sceneType = 9;
        public int windowSize = 600;
        public int amountThreads = 8;
        public string ipAddress = "127.0.0.1";
        public int useRandomPath = 1;
        public string pathConfigFile = "PathCheckpoints/TownCheckPointsConfig.txt";

        private string mLocation;


        public void SaveFile(PeerManager.PeerManager pManager, string pDirectory)
        {
            int index = 0;
            string[] allFiles = Directory.GetDirectories(pDirectory);
            string remainingPath = ConfigurationManager.AppSettings["ConfigFilePath"];

            foreach (string currentPath in allFiles)
            {
                string[] folderName = currentPath.Split('\\');
                string soleFolderName = folderName[folderName.Length - 1];
                if (pManager.mAllStatus.Keys.Contains(folderName[folderName.Length - 1]))
                {
                    string representation = pManager.mAllStatus[soleFolderName].IpPortRepresentation;
                    string fullPath = currentPath + remainingPath;
                    string configFileData = CreateConfigString(index, pManager.mAllPeers[representation].IpAddress.ToString());

                    using(StreamWriter writer = new StreamWriter(fullPath))
                    {
                        writer.Write(configFileData);
                    }

                    index++;
                }
            }
        }

        private string CreateConfigString(int pPeerIndex, string pInitIp)
        {
            StringWriter ss = new StringWriter();
            ss.WriteLine("amountPeers="+AmountPeers);
            ss.WriteLine("peerIndex="+pPeerIndex);
            ss.WriteLine("gatekeeperIp="+gatekeeperIp);
            ss.WriteLine("pathNumber="+pathNumber);
            ss.WriteLine("outputFileName="+outputFileName);
            ss.WriteLine("intervalForBenchmark="+intervalForBenchmark);
            ss.WriteLine("timeAfterCommAtempt="+timeAfterCommAttempt);
            ss.WriteLine("timeSleepAfterFailedAttempt=" + timeSleepAfterFailedAttempt);
            ss.WriteLine("useNetwork="+useNetwork);
            ss.WriteLine("socketNumber="+socketNumber);
            ss.WriteLine("peerName="+peerName);
            ss.WriteLine("sceneType="+sceneType);
            ss.WriteLine("windowSize="+windowSize);
            ss.WriteLine("ipAddress="+pInitIp);
            ss.WriteLine("useRandomPath="+useRandomPath);
            ss.WriteLine("pathConfigFile="+pathConfigFile);
            
            return ss.ToString();
        }


    }
}
